package com.weavernorth.action.AssistantReport;

import com.weavernorth.sysap.dao.SyncOrgStrucDaoImpl;
import org.apache.commons.logging.Log;


import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.UUID;

public class ManagerReport implements Action {
    private static Log log = org.apache.commons.logging.LogFactory.getLog(ManagerReport.class.getName());

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rsSql = new RecordSet();
        RecordSet rsWc = new RecordSet();
        RecordSet rsAsstantSql = new RecordSet();
        RecordSet rsUpdateFw = new RecordSet();
        RecordSet rsUpdateLs = new RecordSet();
        RecordSet rsUpdateCl = new RecordSet();
        RecordSet rs = new RecordSet();
        RecordSet rsJs = new RecordSet();
        log.info("逻辑开始");

        //1：获取表单的requestid
        String strRequestId = requestInfo.getRequestid();
        log.info("表单requestid" + strRequestId);
        //2：获取表单名称tablename
        String strTablename = requestInfo.getRequestManager().getBillTableName();
        log.info("表单名称：" + strTablename);
        //查询表单数据的sql
        String strSql = "select *   from  " + strTablename + " f," + strTablename + "_dt1  d  where  d.mainid=f.id  and  f.requestid = '" + strRequestId + "'";
        log.info("查询表单主表、明细数据的sql:" + strSql);
        rsSql.executeQuery(strSql);
        //流程可能含有多个明细表
        while (rsSql.next()) {
            //获取姓名字段值
            String strXm = Util.null2String(rsSql.getString("xm"));
            log.info("姓名：" + strXm);

            //获取评分类型字段的值
            String strPflx = Util.null2String(rsSql.getString("pflx"));
            log.info("评分类型：" + strPflx);
            //获取总得分字段的值
            double douZdf = Util.getDoubleValue(rsSql.getString("zdf"), 0.0);
            log.info("总得分：" + douZdf);

            //获取分公司的值
            String strFgs = Util.null2String(rsSql.getString("fgs"));
            log.info("分公司：" + strFgs);

            //获取奖金金额
            //查询奖金基数
            String strJsSql = "select dcjs from uf_jjjspzb where  fgs = '" + strFgs + "'";
            rsJs.executeQuery(strJsSql);
            String strJs = "0.0";
            if (rsJs.next()) {
                //获取奖金基数
                strJs = Util.null2String(rsJs.getString("dcjs"));
            }

            //获取店铺名称的值
            String strDpmc = Util.null2String(rsSql.getString("dpmc"));
            log.info("店铺名称：" + strDpmc);
            //获取职位值
            String strZw = Util.null2String(rsSql.getString("zw"));
            log.info("职位：" + strZw);
            //获取工号值
            String strGh = Util.null2String(rsSql.getString("gh"));
            log.info("工号：" + strGh);
            //获取零售经理的值
            String strLsjl = Util.null2String(rsSql.getString("lsjl"));
            log.info("零售经理：" + strLsjl);
            //获取区域经理的值
            String strQyjl = Util.null2String(rsSql.getString("qyjl"));
            log.info("区域经理：" + strQyjl);
            //获取品牌的值
            String strPp = Util.null2String(rsSql.getString("pp"));
            log.info("品牌：" + strPp);
            //获取店铺类型的值
            String strDplx = Util.null2String(rsSql.getString("dplx"));
            log.info("店铺类型：" + strDplx);
            //获取店铺级别的值
            String strDpjb = Util.null2String(rsSql.getString("dpjb"));
            log.info("店铺级别：" + strDpjb);
            //获取店铺状态的值
            String strDpzt = Util.null2String(rsSql.getString("dpzt"));
            log.info("店铺状态：" + strDpzt);
            //获取评分期间的值
            String strPfqj = Util.null2String(rsSql.getString("pfqj"));
            log.info("评分期间：" + strPfqj);
            //获取评分日期的值
            String strPfrq = Util.null2String(rsSql.getString("pfrq"));
            log.info("评分日期：" + strPfrq);
            //查询建模表：店长考核评分汇总表中 是否有 此人
            String strAssistantSql = "select  *  from  uf_dzpfhzb  where  xm ='" + strXm + "' and  pfqj = '" + strPfqj + "'";
            log.info("判断店长建模表中有无此人数据的sql：" + strAssistantSql);
            rsAsstantSql.executeQuery(strAssistantSql);
            //有此人
            if (rsAsstantSql.next()) {
                //获取服务标准的分数
                double douFwbz = Util.getDoubleValue(rsAsstantSql.getString("fwbzpf"), 0.0);
                log.info("有此人时，其服务标准分数：" + douFwbz);
                //获取零售流程的分数
                double douLs = Util.getDoubleValue(rsAsstantSql.getString("lsyypf"), 0.0);
                log.info("有此人时，其零售管理分数：" + douLs);
                //获取陈列管理的分数
                double douClgl = Util.getDoubleValue(rsAsstantSql.getString("clglpf"), 0.0);
                log.info("有此人时，其陈列管理分数：" + douClgl);
                //获取CRM流程的分数
                double douCRM = Util.getDoubleValue(rsAsstantSql.getString("crm"), 0.0);
                log.info("有此人时，其CRM流程的分数：" + douCRM);
                //此数据的id
                String strId = Util.null2String(rsAsstantSql.getString("id"));

                //判断评分类型为服务标准流程
                if (strPflx.equals("2")) {
                    double douFwbzSum = douZdf + douFwbz;
                    //获取奖金金额
                    double douDf = (douFwbzSum + douLs + douClgl + douCRM);
                    double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    String strUpdateAstat = "update uf_dzpfhzb set  fwbzpf = '" + douFwbzSum + "',fs='" + douDf + "', jjje = '" + dubDev + "' where id='" + strId + "'";
                    log.info("更新服务标准流程的sql:" + strUpdateAstat);
                    rsUpdateFw.executeQuery(strUpdateAstat);
                }
                //判断评分类型为CRM流程
                if (strPflx.equals("0")) {
                    double douCRMSum = douZdf + douCRM;
                    //获取奖金金额
                    double douDf = (douCRMSum + douLs + douClgl + douFwbz);
                    double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    String strUpdateAstat = "update uf_dzpfhzb set  crm = '" + douCRMSum + "',fs='" + (douDf) + "', jjje = '" + dubDev + "' where id='" + strId + "'";
                    log.info("更新CRM流程的sql:" + strUpdateAstat);
                    rsUpdateFw.executeQuery(strUpdateAstat);
                }
                //判断评分类型为零售管理流程
                if (strPflx.equals("1")) {
                    double douLsSum = douZdf + douLs;
                    //获取奖金金额
                    double douDf = (douLsSum + douFwbz + douClgl + douCRM);
                    double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    String strUpdateAstat = "update uf_dzpfhzb set  lsyypf = '" + douLsSum + "',fs='" + (douDf) + "', jjje = '" + dubDev + "' where id='" + strId + "'";
                    log.info("更新零售管理流程的sql:" + strUpdateAstat);
                    rsUpdateLs.executeQuery(strUpdateAstat);
                }
                //判断评分类型为陈列管理流程
                if (strPflx.equals("3")) {
                    double douClglSum = douZdf + douClgl;
                    //获取奖金金额
                    double douDf = (douClglSum + douFwbz + douLs + douCRM);
                    double dubJj = (douDf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    String strUpdateAstat = "update uf_dzpfhzb set  clglpf = '" + douClglSum + "',fs='" + (douDf) + "', jjje = '" + dubDev + "' where id='" + strId + "'";
                    log.info("更新陈列管理流程的sql:" + strUpdateAstat);
                    rsUpdateCl.executeQuery(strUpdateAstat);
                }
                //无此人
            } else {
                //判断评分类型为服务标准流程
                if (strPflx.equals("2")) {
                    //获取奖金金额
                    // double intDf = Integer.parseInt(strZdf);
                    double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    //获取uuid,授权需要的参数
                    String strUuid = UUID.randomUUID().toString().replaceAll("-", "");
                    log.info("uuid:" + strUuid);
                    //获取formmodeid
                    int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dzpfhzb");
                    log.info("服务标准流程的formmodeid:" + intModeId);
                    String strInsertAstat = "insert  into  uf_dzpfhzb   (fwbzpf,fs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) values ('" + douZdf + "','" + douZdf + "','" + strFgs + "','" + strDpmc + "','" + strXm + "','" + strZw + "','" + strGh + "','" + strLsjl + "','" + strQyjl + "','" + strPp + "','" + strDplx + "','" + strDpjb + "','" + strDpzt + "','" + strPfqj + "','" + intModeId + "','" + strUuid + "','" + strPfrq + "','" + dubDev + "')";
                    log.info("新增服务标准流程的sql:" + strInsertAstat);
                    rsUpdateFw.executeQuery(strInsertAstat);
                    //授权
                    int intMaxId = 0;
                    rs.executeQuery("select id from uf_dzpfhzb where uuid='" + strUuid + "'");
                    if (rs.next()) {
                        intMaxId = Util.getIntValue((rs.getString("id")), 0);
                    }
                    log.info("授权id:" + intMaxId);
                    ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                    modeRightInfoBanc.setNewRight(true);
                    modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
                }
                //判断评分类型为零售管理流程
                if (strPflx.equals("1")) {
                    //获取奖金金额
                    // double intDf = Integer.parseInt(strZdf);
                    double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    //获取uuid,授权需要的参数
                    String strUuid = UUID.randomUUID().toString().replaceAll("-", "");
                    log.info("uuid:" + strUuid);
                    //获取formmodeid
                    int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dzpfhzb");
                    log.info("零售管理流程的formmodeid:" + intModeId);
                    String strInsertAstat = "insert  into  uf_dzpfhzb   (lsyypf,fs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) values ('" + douZdf + "','" + douZdf + "','" + strFgs + "','" + strDpmc + "','" + strXm + "','" + strZw + "','" + strGh + "','" + strLsjl + "','" + strQyjl + "','" + strPp + "','" + strDplx + "','" + strDpjb + "','" + strDpzt + "','" + strPfqj + "','" + intModeId + "','" + strUuid + "','" + strPfrq + "','" + dubDev + "')";
                    log.info("新增零售管理流程的sql:" + strInsertAstat);
                    rsUpdateLs.executeQuery(strInsertAstat);
                    //授权
                    int intMaxId = 0;
                    rs.executeQuery("select id from uf_dzpfhzb where uuid='" + strUuid + "'");
                    if (rs.next()) {
                        intMaxId = Util.getIntValue((rs.getString("id")), 0);
                    }
                    log.info("授权id:" + intMaxId);
                    ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                    modeRightInfoBanc.setNewRight(true);
                    modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
                }
                //判断评分类型为陈列管理流程
                if (strPflx.equals("3")) {
                    //获取奖金金额
                    //double intDf = Integer.parseInt(strZdf);
                    double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    //获取uuid,授权需要的参数
                    String strUuid = UUID.randomUUID().toString().replaceAll("-", "");
                    log.info("uuid:" + strUuid);
                    //获取formmodeid
                    int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dzpfhzb");
                    log.info("陈列管理流程的formmodeid:" + intModeId);
                    String strInsertAstat = "insert  into  uf_dzpfhzb   (clglpf,fs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) values ('" + douZdf + "','" + douZdf + "','" + strFgs + "','" + strDpmc + "','" + strXm + "','" + strZw + "','" + strGh + "','" + strLsjl + "','" + strQyjl + "','" + strPp + "','" + strDplx + "','" + strDpjb + "','" + strDpzt + "','" + strPfqj + "','" + intModeId + "','" + strUuid + "','" + strPfrq + "','" + dubDev + "')";
                    log.info("新增陈列管理流程的sql:" + strInsertAstat);
                    rsUpdateCl.executeQuery(strInsertAstat);
                    //授权
                    int intMaxId = 0;
                    rs.executeQuery("select id from uf_dzpfhzb where uuid='" + strUuid + "'");
                    if (rs.next()) {
                        intMaxId = Util.getIntValue((rs.getString("id")), 0);
                    }
                    log.info("授权id:" + intMaxId);
                    ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                    modeRightInfoBanc.setNewRight(true);
                    modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
                }
                //判断评分类型为CRM流程
                if (strPflx.equals("0")) {
                    //获取奖金金额
                    //double intDf = Integer.parseInt(strZdf);
                    double dubJj = (douZdf / 100) * Double.parseDouble(strJs);
                    double dubDev = (double) Math.round(dubJj * 100) / 100;
                    //获取uuid,授权需要的参数
                    String strUuid = UUID.randomUUID().toString().replaceAll("-", "");
                    log.info("uuid:" + strUuid);
                    //获取formmodeid
                    int intModeId = SyncOrgStrucDaoImpl.getModeIdByTableName("uf_dzpfhzb");
                    log.info("CRM流程的formmodeid:" + intModeId);
                    String strInsertAstat = "insert  into  uf_dzpfhzb   (crm,fs,fgs,dpmc,xm,zw,gh,lsjl,qyjl,pp,dplx,dpjb,dpzt,pfqj,formmodeid,uuid,pfrq,jjje) values ('" + douZdf + "','" + douZdf + "','" + strFgs + "','" + strDpmc + "','" + strXm + "','" + strZw + "','" + strGh + "','" + strLsjl + "','" + strQyjl + "','" + strPp + "','" + strDplx + "','" + strDpjb + "','" + strDpzt + "','" + strPfqj + "','" + intModeId + "','" + strUuid + "','" + strPfrq + "','" + dubDev + "')";
                    log.info("新增CRM流程的sql:" + strInsertAstat);
                    rsUpdateCl.executeQuery(strInsertAstat);
                    //授权
                    int intMaxId = 0;
                    rs.executeQuery("select id from uf_dzpfhzb where uuid='" + strUuid + "'");
                    if (rs.next()) {
                        intMaxId = Util.getIntValue((rs.getString("id")), 0);
                    }
                    log.info("授权id:" + intMaxId);
                    ModeRightInfo modeRightInfoBanc = new ModeRightInfo();
                    modeRightInfoBanc.setNewRight(true);
                    modeRightInfoBanc.editModeDataShare(1, intModeId, intMaxId);
                }
            }
        }
        log.info("逻辑结束");
        return SUCCESS;
    }
}
